package com.wms.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.entity.Resource;
import com.wms.entity.Role;
import com.wms.service.ResourceService;
import com.wms.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/admin/role")
@RestController("adminRoleController")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private ResourceService resourceService;

    @RequestMapping("/list")
    public Object list(Page<Role> page) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.ne("id", 1);

        OrderItem orderItem = new OrderItem();
        orderItem.setAsc(false);
        orderItem.setColumn("id");
        page.addOrder(orderItem);

        IPage<Role> iPage = roleService.page(page, queryWrapper);
        return success(iPage);
    }

    @RequestMapping("/save")
    public Object save(Role role) {
        if (StringUtils.isEmpty(role.getName())){
            return error("参数不完整");
        }

        roleService.save(role);
        return success();
    }

    @RequestMapping("/detail")
    public Object detail(Long id){
        if (id == null){
            return error("参数不完整");
        }

        Role role = roleService.getById(id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", role.getId());
        map.put("name", role.getName());
        map.put("description", role.getDescription());
        map.put("createDate", role.getCreateDate());
        return success(map);
    }

    @RequestMapping("/update")
    public Object update(Role role) {
        if (role.getId() == null){
            return error("参数不完整");
        }

        roleService.updateById(role);
        return success();
    }


    @RequestMapping("/delete")
    public Object delete(Long id){
        if (id == null){
            return error("参数不完整");
        }

        roleService.deleteById(id);
        return success();
    }

    @RequestMapping("/deleteBatch")
    public Object deleteBatch(@RequestParam(value="ids[]") List<Long> ids) {
        if (ids == null || ids.size() == 0){
            return error("参数不完整");
        }

        roleService.deleteByIds(ids);
        return success();
    }


    @RequestMapping("/bindResources")
    public Object bindResources(Long id, @RequestParam(value = "resourceIds[]") List<Long> resourceIds){
        if (id == null || resourceIds == null || resourceIds.isEmpty()){
            return error("参数不完整");
        }

        Role role = roleService.getById(id);
        if (role == null){
            return error("数据不存在");
        }

        List<Resource> resourceList = new ArrayList<Resource>();
        for (Long resourceId : resourceIds){
            Resource resource = resourceService.getById(resourceId);
            if (resource == null){
                return error("数据不存在");
            }
            resourceList.add(resource);
        }

        roleService.bindResources(role, resourceList);
        return success();
    }




}
